package com.qiangesoft.wechat.core.service.impl;

import com.qiangesoft.wechat.core.service.IWxMaterialService;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 素材服务
 *
 * @author qiangesoft
 * @date 2023-09-13
 */
@Service
public class WxMaterialServiceImpl implements IWxMaterialService {

    @Autowired
    private WxMpService wxMpService;

    @Override
    public String upload(String mediaFileType, String fileType, InputStream inputStream) throws WxErrorException {
        WxMediaUploadResult uploadResult = wxMpService.getMaterialService().mediaUpload(mediaFileType, fileType, inputStream);
        return uploadResult.getMediaId();
    }

    @Override
    public List<String> batchUpload(String mediaFileType, String fileType, List<InputStream> inputStreamList) throws WxErrorException {
        List<String> mediaIdList = new ArrayList<>();
        for (InputStream inputStream : inputStreamList) {
            WxMediaUploadResult uploadResult = wxMpService.getMaterialService().mediaUpload(mediaFileType, fileType, inputStream);
            mediaIdList.add(uploadResult.getMediaId());
        }
        return mediaIdList;
    }
}
